﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace _7.IsolatedStorageKeepDateTimeLastStarted
{
    class Program
    {
        static void Main(string[] args)
        {            
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("isoFile", System.IO.FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                try
                {
                    object o = formatter.Deserialize(isoStream);
                    DateTime lastStarted = (DateTime)o;
                    Console.WriteLine("This program was last started : {0}.", lastStarted);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("This program has never been started before.");
                }

                isoStream.Seek(0, SeekOrigin.Begin);
                formatter.Serialize(isoStream, DateTime.Now);

                IsolatedStorageFile file = IsolatedStorageFile.GetStore(
                    IsolatedStorageScope.User | IsolatedStorageScope.Assembly
                    , null
                    , null);                
            }
        }
    }
}
